﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace yule.Util
{
    /// <summary>
    /// 转化类
    /// </summary>
    public static class Convert
    {
        /// <summary>
        /// 转化为Int
        /// 若转化异常返回(int.MinValue)
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static int ToInt(object obj)
        {
            int result = int.MinValue;

            if (obj != null)
            {
                if (obj.GetType().IsEnum == true)
                {
                    result = obj.GetHashCode();
                }
                else if (int.TryParse(obj.ToString(), out result) == false)
                {
                    result = int.MinValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 转化枚举
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="valueOrName"></param>
        /// <returns></returns>
        public static TEnum ToEnum<TEnum>(string valueOrName) where TEnum : struct
        {
            TEnum t = default(TEnum);

            Enum.TryParse(valueOrName, true, out t);

            return t;
        }

        /// <summary>
        /// 转化枚举
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TEnum ToEnum<TEnum>(int value) where TEnum : struct
        {
            return ToEnum<TEnum>(value.ToString());
        }

        /// <summary>
        /// 转化为String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(object obj)
        {
            if (obj != null && obj.GetType().IsEnum == true)
            {
                return obj.GetHashCode().ToString();
            }
            return obj == null ? null : obj.ToString();
        }

        /// <summary>
        /// 转化为日期
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static System.DateTime ToDateTime(object obj)
        {
            System.DateTime date = System.DateTime.MinValue;

            if (obj != null)
            {
                string dateStr = obj.ToString();
                System.DateTime.TryParse(dateStr, out date);
            }

            return date;
        }

        /// <summary>
        /// 转布尔
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ToBool(object obj)
        {
            return bool.Parse(obj.ToString());
        }

        /// <summary>
        /// 转数字
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object obj)
        {
            decimal d = decimal.MinValue;

            if (obj != null)
            {
                string ds = obj.ToString();
                decimal.TryParse(ds, out d);
            }

            return d;
        }

        /// <summary>
        /// 转数字
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ToDouble(object obj)
        {

            double d = double.MinValue;

            if (obj != null)
            {
                string ds = obj.ToString();
                double.TryParse(ds, out d);
            }

            return d;
        }

        /// <summary>
        /// 转浮点
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static float ToFloat(object obj)
        {

            float d = float.MinValue;

            if (obj != null)
            {
                string ds = obj.ToString();
                float.TryParse(ds, out d);
            }

            return d;
        }
        
        

    }
}
